package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.mingtu.guards.R;

import java.util.HashMap;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class ScreenControlAdapter2 extends RecyclerView.Adapter<ScreenControlAdapter2.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private HashMap<Integer, Boolean> statusMap = new HashMap<>();
    private String[] targetNameArray = {
            "PM2.5", "PM10",
            "负氧离子", "雨量",
            "风速", "风向",
            "温度", "大气压",
            "净辐射", "总辐射",
            "湿度", "水质PH",
            "液位", "浊度",
            "水质电导率", "溶解氧",
            "水温度"};
    private int[] targetIvFlagArray1 = {
            R.mipmap.icon_green_targe_pm25, R.mipmap.icon_green_targe_pm10,
            R.mipmap.icon_green_targe_negative_oxyge_nions, R.mipmap.icon_green_targe_rainfall,
            R.mipmap.icon_green_targe_wind_speed, R.mipmap.icon_green_targe_wind_direction,
            R.mipmap.icon_green_targe_temperature, R.mipmap.icon_green_targe_atmospheric_pressure,
            R.mipmap.icon_green_targe_radiation1, R.mipmap.icon_green_targe_radiation2,
            R.mipmap.icon_green_targe_humidity,  R.mipmap.icon_green_targe_ph,
            R.mipmap.icon_green_targe_liquid_level,  R.mipmap.icon_green_targe_turbidity,
            R.mipmap.icon_green_targe_conductivity,  R.mipmap.icon_green_targe_dissolved_oxygen,
            R.mipmap.icon_green_targe_water_temperature
    };
    private int[] targetIvFlagArray2 = {
            R.mipmap.icon_white_targe_pm25, R.mipmap.icon_white_targe_pm10,
            R.mipmap.icon_white_targe_negative_oxyge_nions, R.mipmap.icon_white_targe_rainfall,
            R.mipmap.icon_white_targe_wind_speed, R.mipmap.icon_white_targe_wind_direction,
            R.mipmap.icon_white_targe_temperature, R.mipmap.icon_white_targe_atmospheric_pressure,
            R.mipmap.icon_white_targe_radiation1, R.mipmap.icon_white_targe_radiation2,
            R.mipmap.icon_white_targe_humidity, R.mipmap.icon_white_targe_ph,
            R.mipmap.icon_white_targe_liquid_level,  R.mipmap.icon_white_targe_turbidity,
            R.mipmap.icon_white_targe_conductivity,  R.mipmap.icon_white_targe_dissolved_oxygen,
            R.mipmap.icon_white_targe_water_temperature
    };
    private boolean isTable = com.mingtu.common.utils.ScreenUtils.isTablet();

    public ScreenControlAdapter2(Context context) {
        this.context = context;
        for (int i = 0; i < targetNameArray.length; i++) {
            statusMap.put(i, false);
        }
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_screen_control, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view, isTable);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {


        boolean status = statusMap.get(position);
        if (status) {
            holder.layout.setBackground(context.getDrawable(R.drawable.shape_bg_15_gradient));
            holder.ivFlag.setImageResource(targetIvFlagArray2[position]);
            holder.tvTargeName.setTextColor(context.getResources().getColor(R.color.color_white));
        } else {
            holder.layout.setBackground(context.getDrawable(R.drawable.shape_bg_15_5ab_stroke));
            holder.ivFlag.setImageResource(targetIvFlagArray1[position]);
            holder.tvTargeName.setTextColor(context.getResources().getColor(R.color.text_color_5ab));
        }
        holder.tvTargeName.setText(targetNameArray[position]);

        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return targetNameArray.length;
    }

    public void upData(int position) {
        for (int i = 0; i < targetNameArray.length; i++) {
            statusMap.put(i, false);
        }
        statusMap.put(position, true);
        notifyDataSetChanged();
    }

    public void resetData() {
        for (int i = 0; i < targetNameArray.length; i++) {
            statusMap.put(i, false);
        }
        notifyDataSetChanged();
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        public LinearLayout layout;
        public ImageView ivFlag;
        public TextView tvTargeName;

        public ActivityHolder(View itemView, boolean isTable) {
            super(itemView);
            convertView = itemView;
            layout = (LinearLayout) convertView.findViewById(R.id.layout);
            ivFlag = (ImageView) convertView.findViewById(R.id.iv_flag);
            tvTargeName = (TextView) convertView.findViewById(R.id.tv_targe_name);


//            ViewGroup.LayoutParams lp = layout.getLayoutParams();
//            int screenWidth = ScreenUtils.getScreenWidth(); //照片一行3个，间隙是20dp
//
//            int mWidth = (screenWidth - SizeUtils.dp2px(15)) / 2;
//            int mHeight = mWidth * SizeUtils.dp2px(73) / SizeUtils.dp2px(165);
//            if (isTable) {
//                mWidth = (screenWidth - SizeUtils.dp2px(15)) / 3;
//            }
//
//            lp.width = mWidth;
//            lp.height = mHeight;
//            layout.setLayoutParams(lp);
        }
    }

}
